<?php

namespace App\Http\Controllers\Backend\Order;

use App\Http\Controllers\Controller;
use App\Http\Repositories\Order\OrderRepo;
use App\Http\Repositories\Relation\OrderObjectRepo;
use App\Http\Requests\Validators\Backend\Order\OrderValidator;
use App\Traits\HelpFunctions;
use Illuminate\Http\Request;

/**
 * 订单相关
 */
class OrderController extends Controller
{
    use HelpFunctions;

    const VALIDATOR = OrderValidator::class;

    public function __construct()
    {
        $this->self_repo = new OrderRepo;
        $this->order_object_repo = new OrderObjectRepo;
    }

    public function search(Request $request)
    {
        //参数验证
        $args = $request->only(
            // 'title',
            // 'province_id',
            // 'city_id',
            // 'area_id',
            // 'begin_price',
            // 'end_price',
            // 'introduction',
            // 'negotiate',
            // 'begin_deadline',
            // 'end_deadline',
            'order_sn',
            'order_id',
            'order_status',
            'order_id',
            'type',
            // 'user_name',
            // 'phone',
            'begin_time',
            'end_time',
            // 'type_ids',
        );

        $this->my_validate($args, 'searchRules');

        return $this->self_repo->search($args);
    }

    public function store(Request $request)
    {
        //参数验证
        $args = $request->only(
            'object_id',
            'type',
        );

        $this->my_validate($args, 'storeRules');

        $args['user_id'] = $this->getUserId();
        $args['created_at'] = date('Y-m-d H:i:s');
        $args['sn'] = $this->uSN('OD', $args['created_at']);
        if ($this->order_object_repo->uniqueExist(['user_id' => $args['user_id'], 'type' => $args['type'], 'object_id' => $args['object_id']])) $this->clientError('已经提交订单成功');

        //添加数据
        return $this->returnResponse($this->self_repo->createOne($args));
    }

    public function delete($id)
    {
        return $this->self_repo->deleteById($id);
    }

    public function edit(Request $request, $id)
    {
        //参数验证
        $args = $request->only(
            'status',
        );
        $this->my_validate($args, 'editRules');

        //根据id修改数据
        return $this->returnResponse($this->self_repo->editById($id, $args));
    }
}
